$(document).ready(function () {
    var search = [];
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/hy/footBfinfo/searchUser",
        success: function (data) {
            search = data;
            $("#selectqd").autocomplete(search, {
                minChars: 0,
                width: $("#selectqd").width() + 10,
                //matchContains: "word",
                autoFill: false,
                formatItem: function (row, i, max) {
                    return row.nike;
                },
                formatMatch: function (row, i, max) {
                    return row.nike;
                },
                formatResult: function (row) {
                    return row.nike;
                }
            });
        }
    });
    $("#selectqd").result(function (event, data, formatted) {
        $("#qd2").val(data.id);
        $("#keTeam").empty();
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/hy/footBfinfo/searchPerson",
            data: "userId=" + data.id,
            success: function (info) {
                $(info).each(function (index, t) {
                    var result = "<td>";
                    if (t.wzbm === 'GK') {
                        result += '<img alt="" src="/images/foot/gk.png"/>';
                    } else if (t.wzbm === 'SW') {
                        result += '<img alt="" src="/images/foot/sw.png"/>';
                    } else if (t.wzbm === 'CB') {
                        result += '<img alt="" src="/images/foot/cb.png"/>';
                    } else if (t.wzbm === 'LB') {
                        result += '<img alt="" src="/images/foot/lb.png"/>';
                    } else if (t.wzbm === 'RB') {
                        result += '<img alt="" src="/images/foot/rb.png"/>';
                    } else if (t.wzbm === 'DMF') {
                        result += '<img alt="" src="/images/foot/dmf.png"/>';
                    } else if (t.wzbm === 'CMF') {
                        result += '<img alt="" src="/images/foot/cmf.png"/>';
                    } else if (t.wzbm === 'LMF') {
                        result += '<img alt="" src="/images/foot/lmf.png"/>';
                    } else if (t.wzbm === 'RMF') {
                        result += '<img alt="" src="/images/foot/rmf.png"/>';
                    } else if (t.wzbm === 'LWF') {
                        result += '<img alt="" src="/images/foot/lwf.png"/>';
                    } else if (t.wzbm === 'RWF') {
                        result += '<img alt="" src="/images/foot/rwf.png"/>';
                    } else if (t.wzbm === 'AMF') {
                        result += '<img alt="" src="/images/foot/amf.png"/>';
                    } else if (t.wzbm === 'SS') {
                        result += '<img alt="" src="/images/foot/ss.png"/>';
                    } else if (t.wzbm === 'CF') {
                        result += '<img alt="" src="/images/foot/cf.png"/>';
                    }
                    result += "</td>";
                    if (t.qydj === '顶级球星') {
                        result += '<td><span style="font-weight: bold; color: orange;">' + t.yw + (t.name === null ? '' : '(' + t.name + ')') + '</span></td>';
                        result += '<td><em class="icon-star text-warning"></em><em class="icon-star text-warning"></em><em class="icon-star text-warning"></em><em class="icon-star text-warning"></em><em class="icon-star text-warning"></em></td>';
                    } else if (t.qydj === '超级球星') {
                        result += '<td><span style="font-weight: bold; color: purple;">' + t.yw + (t.name === null ? '' : '(' + t.name + ')') + '</span></td>';
                        result += '<td><em class="icon-star text-warning"></em><em class="icon-star text-warning"></em><em class="icon-star text-warning"></em><em class="icon-star text-warning"></em></td>';
                    } else if (t.qydj === '洲际球星') {
                        result += '<td><span style="font-weight: bold;color: blue;">' + t.yw + (t.name === null ? '' : '(' + t.name + ')') + '</span></td>';
                        result += '<td><em class="icon-star text-warning"></em><em class="icon-star text-warning"></em><em class="icon-star text-warning"></em></td>';
                    } else if (t.qydj === '普通球星') {
                        result += '<td><span style="font-weight: bold;color: #9dfe5c">' + t.yw + (t.name === null ? '' : '(' + t.name + ')') + '</span></td>';
                        result += '<td><em class="icon-star text-warning"></em><em class="icon-star text-warning"></em></td>';
                    } else {
                        result += '<td><span style="font-weight: bold;color: white">' + t.yw + (t.name === null ? '' : '(' + t.name + ')') + '</span></td>';
                        result += '<td><em class="icon-star text-warning"></em></td>';
                    }
                    result += '<td><input type="text" name="kjq" class="input-mini spinner-input" id="' + t.fupId + '" title=""/></td>';
                    result += '<td><input type="text" name="kzg" class="input-mini spinner-input" id="' + t.fupId + '" title=""/></td>';
                    result += '<td><input type="text" name="kmvp" class="input-mini spinner-input" id="' + t.fupId + '" title=""/></td>';
                    $("#keTeam").append('<tr>' + result + '</tr>');
                });
                spinnerReflush(data.id);
            }
        });
    });
    spinnerReflush($("#qd1").val());
});
var option = {value: 0, min: 0, max: 10, step: 1, btn_up_class: 'btn-info', btn_down_class: 'btn-info'};
var mvpOption = {value: 0, min: 0, max: 1, step: 1, btn_up_class: 'btn-info', btn_down_class: 'btn-info'};

function spinnerReflush(team) {
    if (team === $("#qd1").val()) {
        $("input[name=jq]").ace_spinner(option).on('change', function () {
            changeNum(team, 1, this.value)
        });
        $('input[name=zg]').ace_spinner(option).on('change', function () {
            changeNum(team, 2, this.value)
        });
        $('input[name=mvp]').ace_spinner(mvpOption).on('change', function () {
            saveMvp(team, this)
        });
    } else {
        $("input[name=kjq]").ace_spinner(option).on('change', function () {
            changeNum(team, 1, this.value)
        });
        $('input[name=kzg]').ace_spinner(option).on('change', function () {
            changeNum(team, 2, this.value)
        });
        $('input[name=kmvp]').ace_spinner(mvpOption).on('change', function () {
            saveMvp(team, this)
        });
    }
}

function changeNum(team, type, value) {
    var ids = new Array();
    var nums = new Array();
    if (team === $("#qd1").val()) {
        var num = 0;
        if (type === 1) {
            $("input[name=jq]").each(function (index, el) {
                var id = $(el).attr('id');
                ids.push(id);
                num += parseInt($(el).val());
                nums.push($(el).val());
            });
            $("#jq1").val(num);
        } else if (type === 2) {
            $("input[name=zg]").each(function (index, el) {
                var id = $(el).attr('id');
                ids.push(id);
                nums.push($(el).val());
            });
        }
    } else {
        var num = 0;
        if (type === 1) {
            $("input[name=kjq]").each(function (index, el) {
                var id = $(el).attr('id');
                ids.push(id);
                num += parseInt($(el).val());
                nums.push($(el).val());
            });
            $("#jq2").val(num);
        } else if (type === 2) {
            $("input[name=kzg]").each(function (index, el) {
                var id = $(el).attr('id');
                ids.push(id);
                nums.push($(el).val());
            });
        }
    }
    saveTemp(team, type, ids, nums);
}

function saveTemp(team, type, ids, nums) {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/hy/footBfinfo/saveTemp",
        data: {"team": team, "type": type, "ids": ids.join(","), "nums": nums.join(",")},
        success: function (data) {

        }
    });
}

function saveMvp(team, obj) {
    var result = false;
    var times = 0;
    $("input[name=mvp]").each(function (index, el) {
        if ($(el).val() === "1") {
            result = true;
            times = times + 1;
        }
    });
    $("input[name=kmvp]").each(function (index, el) {
        if ($(el).val() === "1") {
            result = true;
            times = times + 1;
        }
    });
    if (!result) {
        $("#qd1error").html("每场必须拥有一个MVP");
        return false;
    }
    if (times > 1) {
        $("#qd1error").html("每场最多拥有一个MVP");
        return false;
    }
    $("#qd1error").html("");
    if (obj.value === "1") {
        console.log(obj.id);
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/hy/footBfinfo/saveTempMvp",
            data: {"team": team, "id": obj.id},
            success: function (data) {

            }
        });
    }
}